use "$data\4_individual_ano_reg_3.dta", clear

global outcomes    aminwage wrcon  sosec   empquality_ind lwage
global het_ind wage_q

* Panel A: Main Outcomes (ITT)
	eststo clear
	
		use "$data\4_individual_ano_reg_3.dta", clear
		local c=1 
		foreach o of global outcomes {
			local d=`c'+1
			eststo m`c'`x': areg `o' assignment `o'_2021 $controls_balance missing_`o'_2021 i.wave $se_indiv
			gen inc_in_reg_`o' = e(sample)
		local ++c 
		}
	
	
gen  wage_baseline = lwage_raw_2021 if lwage_raw_2021 != 0

foreach o of global outcomes {
	xtile wage_q_`o' = wage_baseline if inc_in_reg_`o' ==1 , nq(4) 
}
		
estimates clear 

ren assignment Treat
local j = 1
foreach h of global het_ind {
    foreach o of varlist aminwage wrcon sosec empquality_ind lwage {

        local current_outcome = "`o'"
        rename wage_q_`current_outcome' wage_q
		
		ta `h'
		local cats=`r(r)'

     	areg `o' i.Treat##i.`h' `o'_2021 $controls_balance missing_`o'_2021 i.wave $se_indiv  //  [pweight=$weight]  
		eststo m_`o':	margins `h', dydx(1.Treat) post coeflegend
	
		
		

		* N 
		di "Save N and control group means in macro to include in table" 
		sum `o' if e(sample)
		local ${tbl}_n`j'=	r(N)
			
		* add to estimates 
		estadd 	local 		N_`j' `${tbl}_n`j'':m_`o'
		
		// Now for categories
		forv i=1/`cats' {
		di "sum `o' if `h'==`i' & e(sample)"
			sum `o' if `h'==`i' & e(sample)
			local ${tbl}_n`j'_`i'=r(N)

			sum `o' if Treat==0 & `h'==`i' & e(sample)
			local ${tbl}_cm`j'_`i'=round(r(mean),.001)
			
			* add to estimates 
			estadd 	local 		N_`i'_`j' `${tbl}_n`j'_`i'':m_`o'
			local 	cm_`i' 	= 	string(`${tbl}_cm`j'_`i'', "%9.3f")
			estadd local 		cm_`i'_`j' "`cm_`i''":m_`o'
			
			di "Control group mean: `${tbl}_cm`j'_`i''"
			di "Control group mean: `cm_`i''"
			
			
			}
			
		
		*** Case of 4 categories
		if `cats'==4 {
		di "4 Categories: test for differences in treatment effects"

		** Conduct tests 
		// 2 vs. 1 and 3 vs. 1 are simply the coefficients of the interaction terms
		test _b[1.Treat:2.`h'] =_b[1.Treat:1.`h']
		local ${tbl}_p`j'_12=round(r(p),.001)
		di "P-val.\ for diff.\ in coeff.\ 1 and 2: `${tbl}_p`j'_12'"
		
		// 3 vs. 1 
		test _b[1.Treat:3.`h'] =_b[1.Treat:1.`h']
		local ${tbl}_p`j'_13=round(r(p),.001)
		di "P-val.\ for diff.\ in coeff.\ 1 and 3: `${tbl}_p`j'_13'"
		
		// 4 vs. 1 
		test _b[1.Treat:4.`h'] =_b[1.Treat:1.`h']
		local ${tbl}_p`j'_14=round(r(p),.001)
		di "P-val.\ for diff.\ in coeff.\ 1 and 4: `${tbl}_p`j'_14'"
		
		// 2 vs. 3 is test for significant difference in coefficients of interaction terms 
		test _b[1.Treat:2.`h'] = _b[1.Treat:3.`h'] 
		local ${tbl}_p`j'_23=round(r(p),.001)
		di "P-val.\ for diff.\ in coeff.\ 2 and 3: `${tbl}_p`j'_23'"
		
		// 2 vs. 4
		test _b[1.Treat:2.`h'] = _b[1.Treat:4.`h'] 
		local ${tbl}_p`j'_24=round(r(p),.001)
		di "P-val.\ for diff.\ in coeff.\ 2 and 4: `${tbl}_p`j'_24'"
		
		// 3 vs. 4 
		test _b[1.Treat:3.`h'] = _b[1.Treat:4.`h'] 
		local ${tbl}_p`j'_34=round(r(p),.001)
		di "P-val.\ for diff.\ in coeff.\ 3 and 4: `${tbl}_p`j'_34'"
		

		** Add interaction to stored estimates 
		local interaction_12 = string(`${tbl}_p`j'_12', "%9.3f")
		local interaction_13 = string(`${tbl}_p`j'_13', "%9.3f")
		local interaction_14 = string(`${tbl}_p`j'_14', "%9.3f")
		local interaction_23 = string(`${tbl}_p`j'_23', "%9.3f")
		local interaction_24 = string(`${tbl}_p`j'_24', "%9.3f")
		local interaction_34 = string(`${tbl}_p`j'_34', "%9.3f")

		estadd local interaction_12_`j' `interaction_12':m_`o'
		estadd local interaction_13_`j' `interaction_13':m_`o'
		estadd local interaction_14_`j' `interaction_14':m_`o'
		estadd local interaction_23_`j' `interaction_23':m_`o'
		estadd local interaction_24_`j' `interaction_24':m_`o'
		estadd local interaction_34_`j' `interaction_34':m_`o'

		estimates table m*
	}	


        rename wage_q wage_q_`current_outcome'

        local ++j
        local j = `j' - 1

        esttab m* using "$results\01_tables\Table_5_hetero_ind_wage.tex", ///
            keep(1.Treat:1.`h') ///
            scalars("cm_1_`j' Mean" "N_1_`j' N") ///
            b(%9.3f)  se sfmt(%9.3f) noobs label ///
            mgroups("Min. Wage" "Written Contract" "Social Security" "Formality" "Log. Wage", pattern(1 1 1 1 1 1) prefix(\multicolumn{@span}{c}{) suffix(}) span erepeat(\cmidrule(lr){@span})) ///
            starlevels(* 0.10 ** 0.05 *** 0.01) nonumbers nomtitles nonotes replace

        esttab m* using "$results\01_tables\Table_5_hetero_ind_wage.tex", ///
            keep(1.Treat:2.`h') ///
            scalars("cm_2_`j' Mean" "N_2_`j' N") ///
            b(%9.3f)  se sfmt(%9.3f) noobs label ///
            mgroups("Min. Wage" "Written Contract" "Social Security" "Formality" "Log. Wage", pattern(1 1 1 1 1 1) prefix(\multicolumn{@span}{c}{) suffix(}) span erepeat(\cmidrule(lr){@span})) ///
            starlevels(* 0.10 ** 0.05 *** 0.01) nonumbers nomtitles nonotes append

	 esttab m* using "$results\01_tables\Table_5_hetero_ind_wage.tex", ///
            keep(1.Treat:3.`h') ///
            scalars("cm_3_`j' Mean" "N_3_`j' N") ///
            b(%9.3f)  se sfmt(%9.3f) noobs label ///
            mgroups("Min. Wage" "Written Contract" "Social Security" "Formality" "Log. Wage", pattern(1 1 1 1 1 1) prefix(\multicolumn{@span}{c}{) suffix(}) span erepeat(\cmidrule(lr){@span})) ///
            starlevels(* 0.10 ** 0.05 *** 0.01) nonumbers nomtitles nonotes append

			esttab m* using "$results\01_tables\Table_5_hetero_ind_wage.tex", ///
            keep(1.Treat:4.`h') ///
            scalars("cm_4_`j' Mean" "N_4_`j' N" "interaction_12_`j' P-val Diff. between group 1 and 2" "interaction_13_`j' P-val Diff. between group 1 and 3" "interaction_14_`j' P-val Diff. between group 1 and 4" "interaction_23_`j' P-val Diff. between group 2 and 3" "interaction_24_`j' P-val Diff. between group 2 and 4" "interaction_34_`j' P-val Diff. between group 3 and 4") ///
            b(%9.3f)  se sfmt(%9.3f) noobs label ///
            mgroups("Min. Wage" "Written Contract" "Social Security" "Log. Wage", pattern(1 1 1 1 1 1) prefix(\multicolumn{@span}{c}{) suffix(}) span erepeat(\cmidrule(lr){@span})) ///
            starlevels(* 0.10 ** 0.05 *** 0.01) nonumbers nomtitles nonotes append

			     esttab m* , ///
            keep(1.Treat:1.`h') ///
            scalars("cm_1_`j' Mean" "N_1_`j' N") ///
            b(%9.3f)  se sfmt(%9.3f) noobs label ///
            mgroups("Min. Wage" "Written Contract" "Social Security" "Formality" "Log. Wage", pattern(1 1 1 1 1 1) prefix(\multicolumn{@span}{c}{) suffix(}) span erepeat(\cmidrule(lr){@span})) ///
            starlevels(* 0.10 ** 0.05 *** 0.01) nonumbers nomtitles nonotes replace

        esttab m* , ///
            keep(1.Treat:2.`h') ///
            scalars("cm_2_`j' Mean" "N_2_`j' N") ///
            b(%9.3f)  se sfmt(%9.3f) noobs label ///
            mgroups("Min. Wage" "Written Contract" "Social Security" "Formality" "Log. Wage", pattern(1 1 1 1 1 1) prefix(\multicolumn{@span}{c}{) suffix(}) span erepeat(\cmidrule(lr){@span})) ///
            starlevels(* 0.10 ** 0.05 *** 0.01) nonumbers nomtitles nonotes replace

	 esttab m* , ///
            keep(1.Treat:3.`h') ///
            scalars("cm_3_`j' Mean" "N_3_`j' N") ///
            b(%9.3f)  se sfmt(%9.3f) noobs label ///
            mgroups("Min. Wage" "Written Contract" "Social Security" "Formality" "Log. Wage", pattern(1 1 1 1 1 1) prefix(\multicolumn{@span}{c}{) suffix(}) span erepeat(\cmidrule(lr){@span})) ///
            starlevels(* 0.10 ** 0.05 *** 0.01) nonumbers nomtitles nonotes replace

			esttab m* , ///
            keep(1.Treat:4.`h') ///
            scalars("cm_4_`j' Mean" "N_4_`j' N" "interaction_12_`j' P-val Diff. between group 1 and 2" "interaction_13_`j' P-val Diff. between group 1 and 3" "interaction_14_`j' P-val Diff. between group 1 and 4" "interaction_23_`j' P-val Diff. between group 2 and 3" "interaction_24_`j' P-val Diff. between group 2 and 4" "interaction_34_`j' P-val Diff. between group 3 and 4") ///
            b(%9.3f)  se sfmt(%9.3f) noobs label ///
            mgroups("Min. Wage" "Written Contract" "Social Security" "Log. Wage", pattern(1 1 1 1 1 1) prefix(\multicolumn{@span}{c}{) suffix(}) span erepeat(\cmidrule(lr){@span})) ///
            starlevels(* 0.10 ** 0.05 *** 0.01) nonumbers nomtitles nonotes replace
    }
}



